package leetcode;

public class MergeSortedArray88 {
    // [1,2,3,0,0,0] [2,5,6]
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] array = new int[m + n];
        int p1 = 0;
        int p2 = 0;
        int k = 0;
        while (p1 <= m - 1 && p2 <= n - 1) {
            if (nums1[p1] < nums2[p2]) {
                array[k] = nums1[p1];
                k++;
                p1++;
            } else {
                array[k] = nums2[p2];
                k++;
                p2++;
            }
        }
        if (p1 > m - 1) {
            for (int i = p2; i <= n - 1; i++) {
                array[k] = nums2[i];
                k++;
            }
        }
        if (p2 > n - 1) {
            for (int i = p1; i <= m - 1; i++) {
                array[k] = nums1[i];
                k++;
            }
        }
        for(int i=0;i<array.length;i++){
            nums1[i] = array[i];
        }
    }

    public static void main(String[] args) {
        MergeSortedArray88 m =new MergeSortedArray88();
        m.merge(new int[]{1,2,3,0,0,0},3,new int[]{2,5,6},3);
    }
}
